package com.bignerdranch.travelcommunity.util

<<<<<<< HEAD
import android.content.Context
import android.net.ConnectivityManager
import com.bignerdranch.tclib.LogUtil
=======
import java.io.IOException
import java.io.OutputStream
import java.net.HttpURLConnection
import java.net.URL


>>>>>>> f9d31c9... first

/**
 * @author zhongxinyu
 * @date 2020/4/10
 * GitHub:https://github.com/ZXY-stu/TravelCommunity.git
 */
class NetUtil {

<<<<<<< HEAD


    companion object {
        /**
         * 没有连接网络
         */
        private const val NETWORK_NONE = -1
        /**
         * 移动网络
         */
        private const val NETWORK_MOBILE = 0
        /**
         * 无线网络
         */
        private const val NETWORK_WIFI = 1

        fun getNetWorkState(context: Context): Int { // 得到连接管理器对象
            val connectivityManager = context
                .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

            LogUtil.e("nee")

            val activeNetworkInfo = connectivityManager
                .activeNetworkInfo
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected) {
                if (activeNetworkInfo.type == ConnectivityManager.TYPE_WIFI) {
                    return NETWORK_WIFI
                } else if (activeNetworkInfo.type == ConnectivityManager.TYPE_MOBILE) {
                    return NETWORK_MOBILE
                }
            } else {
                return NETWORK_NONE
            }

            return NETWORK_NONE
        }
    }
=======
   companion object{
       fun getInstance():NetUtil{
           return NetUtil()
       }
   }

   private  val  connectTimeoutMillis = 1000
    private val readTimeoutMillis = 1000
    private val host = "127.0.0.1";
    private val acceptEncoding = "gzip"
    private val connectionStatus = "Keep-Alive";
    private val userAgent = "Dalvik/2.1.0 (Linux; U; Android 9; SM-G9650 Build/PPR1.180610.011)"
    private  val requestProperties = mapOf(
        "Host" to host,
        "Accept-Encoding" to acceptEncoding,
        "Connection" to connectionStatus,
       "User-Agent" to userAgent
    )




    /**
     * GET /ping HTTP/1.1
       User-Agent: Dalvik/2.1.0 (Linux; U; Android 9; SM-G9650 Build/PPR1.180610.011)
       Host: 127.0.0.1:37893
       Connection: Keep-Alive
       Accept-Encoding: gzip
     * */



    @Throws(IOException::class)
    fun makeConnection(url: URL, postBody: ByteArray?, position: Long,
        length: Long, followRedirects: Boolean
    ): HttpURLConnection? {
        val connection: HttpURLConnection = url.openConnection() as HttpURLConnection
        connection.setConnectTimeout(connectTimeoutMillis)
        connection.setReadTimeout(readTimeoutMillis)
        synchronized(requestProperties) {
            for ((key, value) in requestProperties) {
                connection.setRequestProperty(key, value)
            }
        }
        if (!(position == 0L && length == C.LENGTH_UNBOUNDED.toLong())) {
            var rangeRequest = "bytes=$position-"
            if (length != C.LENGTH_UNBOUNDED.toLong()) {
                rangeRequest += position + length - 1
            }
            connection.setRequestProperty("Range", rangeRequest)
        }

        connection.setInstanceFollowRedirects(followRedirects)
        connection.setDoOutput(postBody != null)
        if (postBody != null) {
            connection.setFixedLengthStreamingMode(postBody.size)
            connection.connect()
            val os: OutputStream = connection.getOutputStream()
            os.write(postBody)
            os.close()
        } else {
            connection.connect()
        }
        return connection
    }



>>>>>>> f9d31c9... first
}